package com.model.basemodel

import android.content.Context
import android.content.Intent
import android.os.Handler
import android.os.Message
import android.support.v4.app.FragmentTransaction
import android.util.Log
import android.view.KeyEvent
import android.widget.RadioGroup
import android.widget.Toast
import com.model.basemodel.beans.ResponseBase
import com.model.basemodel.beans.Test
import com.model.basemodel.beans.Test2
import com.model.basemodel.beans.response.TestBase
import com.model.basemodel.ui.activity.base.BaseActivity
import com.model.basemodel.ui.fragment.MineFragment
import com.model.basemodel.ui.fragment.MyHomeFragment
import com.model.basemodel.ui.fragment.ServiceFragment
import com.model.basemodel.util.showToast
import kotlinx.android.synthetic.main.activity_main.*
import org.jetbrains.anko.intentFor

/**
 * 主Activity
 */
class MainActivity : BaseActivity(), RadioGroup.OnCheckedChangeListener {

    companion object {
        fun actionStart(context: Context) {
            val intent = Intent(context, MainActivity::class.java)
            context.startActivity(intent)
        }
    }

    override val title: String by lazy {
        ""
    }
    override val layoutResId: Int = R.layout.activity_main
    override val isStatusBarTransient: Boolean = true
    var mineFragment: MineFragment? = null
    var myHomeFragment: MyHomeFragment? = null
    var serviceFragment: ServiceFragment? = null

    // 定义一个变量，来标识是否退出
    private var isExit = false
    var mHandler: Handler = object : Handler() {
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            isExit = false
        }
    }

    override fun getIntentMessageData() {
    }

    override fun initView() {
        rd_group.setOnCheckedChangeListener(this)
        rd_menu_credit.isChecked = true
    }

    override fun initData() {
    }

    override fun onCheckedChanged(group: RadioGroup?, checkedId: Int) {
        val transaction: FragmentTransaction? =
            supportFragmentManager.beginTransaction()
        hideAllFragment(transaction)//隐藏所有Fragment
        when (checkedId) {
            R.id.rd_menu_credit -> {//我的家
                if (myHomeFragment == null) {
                    myHomeFragment = MyHomeFragment.newInstance()
                    transaction?.add(R.id.fl_container, myHomeFragment!!)
                } else {
                    transaction?.show(myHomeFragment!!)
                }
            }
            R.id.rd_menu_service -> {//智能
                if (serviceFragment == null) {
                    serviceFragment = ServiceFragment.newInstance()
                    transaction?.add(R.id.fl_container, serviceFragment!!)
                } else {
                    transaction?.show(serviceFragment!!)
                }
            }
            R.id.rd_menu_mine -> {//我的
                if (mineFragment == null) {
                    mineFragment = MineFragment.newInstance()
                    transaction?.add(R.id.fl_container, mineFragment!!)
                } else {
                    transaction?.show(mineFragment!!)
                }
            }
        }
        transaction?.commitAllowingStateLoss()
    }

    fun hideAllFragment(transaction: FragmentTransaction?) {
        if (myHomeFragment != null) {
            transaction?.hide(myHomeFragment!!)
        }
        if (mineFragment != null) {
            transaction?.hide(mineFragment!!)
        }
        if (serviceFragment != null) {
            transaction?.hide(serviceFragment!!)
        }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit()
            return false
        }
        return super.onKeyDown(keyCode, event)
    }

    private fun exit() {
        if (!isExit) {
            isExit = true
            Toast.makeText(
                applicationContext, "再按一次退出应用",
                Toast.LENGTH_SHORT
            ).show()
            // 利用handler延迟发送更改状态信息
            mHandler.sendEmptyMessageDelayed(0, 2000)
        } else {
            finish()
        }
    }


    override fun onEvent(event: Any) {
        super.onEvent(event)
        when (event) {
            is ResponseBase<*> -> {
                when (event.data) {
                    is ArrayList<*> -> {
                        var array: ArrayList<TestBase> = event.data as ArrayList<TestBase>
                        Log.i("========Main=", array[0].title)
                    }
                }
            }
            is Test -> {
                Log.i("-----------========", "MainListActivity recend Test")
            }
            is Test2 -> {
                Log.i("-----------========", "MainListActivity recend Test2")
            }
        }
    }

}